
import QtQuick 2.14
import QtQuick.Controls 1.4
import QtQuick.Layouts 1.14

Item {
    id:root
    implicitWidth: 130
    implicitHeight: 200

    property bool    isSelected: false
    property string  source : ""
    property string  name : ""
    property string  type : "file" //类型
    property string  abs_path: ""
    property string  cur_path: ""
    property int  btnType : 0
    property int  curViewSize:-1
    property var fileTypeMap : {
                                    "dir":"qrc:/img/dir.png",
                                    "jpg":"qrc:/img/wait.png",
                                    "unkonw":"qrc:/img/untype.png",
                                    "file":"qrc:/img/untype.png"
                                }

    signal lastTapFile(var fileName)

    onCurViewSizeChanged: {
        if("dir" == root.type){
            return;
        }
        if("" === printControl.getIconUUId(root.abs_path))
            return;

        root.isSelected = printControl.getFileSelected(root.name)
        fileIcon.source = "file:///mnt/nvme/qmlPrint/" + printControl.getIconUUId(root.abs_path) + ".tif"
    }

    Component.onCompleted: {
        if("dir" == root.type) {
            return;
        }

        if(printControl.selectPicture){
            selectRec.visible = true
            fileBox.color  = "#C0C0C0"
            fileBox.opacity = .5
            if(root.isSelected){
                fileBox.opacity = 1
                selectRec.color = "#1989FA"
            }
        }else{
            selectRec.visible = false
            selectRec.color = "#fff"
            root.isSelected = false
            fileBox.opacity = 1
            fileBox.color  = "#fff"
        }
    }

    Rectangle{
        anchors.fill: parent
        Rectangle{
            id:fileBox
            radius : 4
            //clip: true
            implicitHeight: 125
            implicitWidth: 125
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.verticalCenter: parent.verticalCenter
            anchors.verticalCenterOffset: -20
            AnimatedImage {
                id: fileIcon
                source:root.fileTypeMap[type]
                cache:false
                asynchronous: false
                //fillMode: Image.PreserveAspectFit
                // sourceSize: Qt.size(110,110)
                width: 110
                height: 110
                anchors.centerIn: parent
            }

            Connections{
                target: printControl
                function onSmallPicRefresh(){
                    if("dir" == root.type)
                        return;

                    if("" === printControl.getIconUUId(root.abs_path) && 1 === fileIcon.status){
                        return;
                    }

                    fileIcon.source = "file:///mnt/nvme/qmlPrint/" + printControl.getIconUUId(root.abs_path) + ".tif"
                }
            }
        }

        Rectangle{
           id:selectRec
           width: 30
           height: 30
           radius: .5 * width
           visible: false
           anchors.bottom: fileBox.bottom
           anchors.right: fileBox.right
           anchors.rightMargin: 6
           anchors.bottomMargin: 6
           border.color: '#1989FA'
           border.width: 1

           Image {
               id: name
               width: 35
               height: 40
               source: "qrc:/imgs/seclected.png"
               anchors.centerIn: parent
           }
        }

        Text {
            id:fileName
            width: parent.width
            text: root.name
            //elide: Text.ElideLeft
            horizontalAlignment: Text.AlignHCenter
            wrapMode: Text.WordWrap
            elide: Text.ElideRight
            maximumLineCount: 2
            font{
                family: "Microsoft YaHei"
                pixelSize: 20
            }
            anchors.top: fileBox.bottom
            anchors.horizontalCenter: parent.horizontalCenter
        }
    }

    Connections{
        target: printControl
        function onSelectPictureChanged(){
            if("dir" == root.type) {
                return;
            }

            if(printControl.selectPicture){
                selectRec.visible = true
                fileBox.color  = "#C0C0C0"
                fileBox.opacity = .5
                if(root.isSelected){
                    fileBox.opacity = 1
                    selectRec.color = "#1989FA"
                }
            }else{
                selectRec.visible = false
                selectRec.color = "#fff"
                root.isSelected = false
                fileBox.opacity = 1
                fileBox.color  = "#fff"
            }
         }
     }

    MouseArea{
        anchors.fill: root
        propagateComposedEvents: false
        pressAndHoldInterval:300
        onPressAndHold: {
            root.isSelected = true
            printControl.selectPicture = true;
        }

        onClicked: {
            if("dir" == root.type || printControl.selectPicture){
                return;
            }
            printControl.touchPrintFiles(root.abs_path);
            root.lastTapFile(root.abs_path)
        }

        onPressed: {
            if("dir" == root.type){
                return;
            }
            if(!printControl.selectPicture){
               return;
            }

            if(!root.isSelected){
                selectRec.color = "#1989FA"
                fileBox.opacity = 1
                root.isSelected = true
            }else{
                fileBox.color  = "#C0C0C0"
                fileBox.opacity = .5
                selectRec.color = "#fff"
                root.isSelected = false
            }
        }

        onReleased: {
            if("dir" == root.type){
                printControl.getNext_DirInfo(root.name);
                printControl.selectPicture = false;
            }else{
                if(!printControl.selectPicture){
                   return;
                }
                printControl.addFilesToList(root.abs_path,root.isSelected);
                printControl.setFileSelected(root.name,root.isSelected)
                root.lastTapFile(root.abs_path)
            }
        }
    }
}

